﻿using System;

namespace PentagoAg
{
    // lets you play vs the AI on the command line
    public class ConsolePlayer : Player
    {
        public ConsolePlayer(PieceColor c)
            : base(c)
        {
        }

        public override Board Play(Board b)
        {
            Console.WriteLine(b.ToString());
            Console.WriteLine();
            Console.WriteLine(
                "  --- --- " + "\r\n" +
                "5| c>|<a |" + "\r\n" +
                "4|a2 | 1c|" + "\r\n" +
                "3|v  |  v|" + "\r\n" +
                "  --- --- " + "\r\n" +
                "2| c>|<a |" + "\r\n" +
                "1|a3 | 4c|" + "\r\n" +
                "0|v  |  v|" + "\r\n" +
                "  --- --- " + "\r\n" +
                "  012 345 "
                );

            String input;
            int xCoord;
            int yCoord;
            String rotation;
            Rotation r;

            while (true)
            {
                Console.WriteLine("Enter move. Format: <xCoord> <yCoord> <quadrant><rotation direction>");
                input = Console.ReadLine();
                String[] split = input.Split(' ');

                try
                {
                    xCoord = Int32.Parse(split[0]);
                    yCoord = Int32.Parse(split[1]);
                    rotation = split[2];
                }
                catch (Exception)
                {
                    continue;
                }

                switch (xCoord)
                {
                    case 0: case 1: case 2: case 3: case 4: case 5:
                        break;
                    default:
                        continue;
                }
                switch (yCoord)
                {
                    case 0: case 1: case 2: case 3: case 4: case 5:
                        break;
                    default:
                        continue;
                }

                switch (rotation.ToLower())
                {
                    case "1a":
                        r = Rotation.UpperRightAntiClockwise;
                        break;
                    case "1c":
                        r = Rotation.UpperRightClockwise;
                        break;
                    case "2a":
                        r = Rotation.UpperLeftAntiClockwise;
                        break;
                    case "2c":
                        r = Rotation.UpperLeftClockwise;
                        break;
                    case "3a":
                        r = Rotation.LowerLeftAntiClockwise;
                        break;
                    case "3c":
                        r = Rotation.LowerLeftClockwise;
                        break;
                    case "4a":
                        r = Rotation.LowerRightAntiClockwise;
                        break;
                    case "4c":
                        r = Rotation.LowerRightClockwise;
                        break;
                    default:
                        continue;
                }

                Board returnVal = new Board(b);

                if (returnVal.ApplyMove(xCoord, yCoord, this.Color, r))
                    return returnVal;
            }
        }
    }
}
